<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="类型:route=路由,menu_dir=菜单目录,menu=菜单项" prop="type">-->
<!--        <el-select v-model="queryParams.type" placeholder="请选择类型:route=路由,menu_dir=菜单目录,menu=菜单项" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in route_type"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="规则名称" prop="name">-->
<!--        <el-input-->
<!--          v-model="queryParams.name"-->
<!--          placeholder="请输入规则名称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="路由路径" prop="path">-->
<!--        <el-input-->
<!--          v-model="queryParams.path"-->
<!--          placeholder="请输入路由路径"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="图标" prop="icon">-->
<!--        <el-input-->
<!--          v-model="queryParams.icon"-->
<!--          placeholder="请输入图标"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="菜单类型:tab=选项卡,link=链接,iframe=Iframe" prop="menuType">-->
<!--        <el-select v-model="queryParams.menuType" placeholder="请选择菜单类型:tab=选项卡,link=链接,iframe=Iframe" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in menu_type"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="Url" prop="url">-->
<!--        <el-input-->
<!--          v-model="queryParams.url"-->
<!--          placeholder="请输入Url"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="组件路径" prop="component">-->
<!--        <el-input-->
<!--          v-model="queryParams.component"-->
<!--          placeholder="请输入组件路径"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="权重(排序)" prop="weigh">-->
<!--        <el-input-->
<!--          v-model="queryParams.weigh"-->
<!--          placeholder="请输入权重(排序)"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="状态:0=禁用,1=启用" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择状态:0=禁用,1=启用" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="更新时间" prop="updatetime">-->
<!--        <el-input-->
<!--          v-model="queryParams.updatetime"-->
<!--          placeholder="请输入更新时间"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建时间" prop="createtime">-->
<!--        <el-input-->
<!--          v-model="queryParams.createtime"-->
<!--          placeholder="请输入创建时间"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['userInfo:rule:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['userInfo:rule:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['userInfo:rule:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['userInfo:rule:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ruleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="id" />-->
<!--      <el-table-column label="上级菜单" align="center" prop="pid" />-->
<!--      <el-table-column label="类型:route=路由,menu_dir=菜单目录,menu=菜单项" align="center" prop="type">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :options="route_type" :value="scope.row.type"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="标题" align="center" sortable prop="title" />
      <el-table-column label="图标" align="center" sortable prop="icon" />
      <el-table-column label="规则名称" align="center" sortable prop="name" />
<!--      <el-table-column label="路由路径" align="center" prop="path" />-->
      <el-table-column label="菜单类型" align="center" sortable prop="menuType">
        <template #default="scope">
          <dict-tag :options="menu_type" :value="scope.row.menuType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="Url" align="center" prop="url" />-->
<!--      <el-table-column label="组件路径" align="center" prop="component" />-->
<!--      <el-table-column label="扩展属性:none=无,add_rules_only=只添加为路由,add_menu_only=只添加为菜单" align="center" prop="extend">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :options="extended_attribute" :value="scope.row.extend"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
<!--      <el-table-column label="权重(排序)" align="center" prop="weigh" />-->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="changeSwitchStatus($event,scope.row)"
          />
          <!-- <dict-tag :options="status" :value="scope.row.status"/> -->
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" sortable prop="updatetime" />
      <el-table-column label="创建时间" align="center" sortable prop="createtime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['userInfo:rule:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['userInfo:rule:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员规则管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="ruleRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in route_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="规则名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入规则名称" />
        </el-form-item>
        <el-form-item label="路由路径" prop="path">
          <el-input v-model="form.path" placeholder="请输入路由路径" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入图标" />
        </el-form-item>
        <el-form-item label="菜单类型" prop="menuType">
          <el-select v-model="form.menuType" placeholder="请选择菜单类型">
            <el-option
              v-for="dict in menu_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="Url" prop="url">
          <el-input v-model="form.url" placeholder="请输入Url" />
        </el-form-item>
        <el-form-item label="组件路径" prop="component">
          <el-input v-model="form.component" placeholder="请输入组件路径" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="权重(排序)" prop="weigh">
          <el-input v-model="form.weigh" placeholder="请输入权重(排序)" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatetime">
          <el-input v-model="form.updatetime" placeholder="请输入更新时间" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createtime">
          <el-input v-model="form.createtime" placeholder="请输入创建时间" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Rule">
import { listRule, getRule, delRule, addRule, updateRule } from "@/api/userInfo/rule";

const { proxy } = getCurrentInstance();
const { status, menu_type, route_type } = proxy.useDict('status', 'menu_type', 'route_type');

const ruleList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    pid: null,
    type: null,
    title: null,
    name: null,
    path: null,
    icon: null,
    menuType: null,
    url: null,
    component: null,
    extend: null,
    weigh: null,
    status: null,
    updatetime: null,
    createtime: null
  },
  rules: {
    pid: [
      { required: true, message: "上级菜单不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "类型:route=路由,menu_dir=菜单目录,menu=菜单项不能为空", trigger: "change" }
    ],
    title: [
      { required: true, message: "标题不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "规则名称不能为空", trigger: "blur" }
    ],
    path: [
      { required: true, message: "路由路径不能为空", trigger: "blur" }
    ],
    icon: [
      { required: true, message: "图标不能为空", trigger: "blur" }
    ],
    menuType: [
      { required: true, message: "菜单类型", trigger: "change" }
    ],
    url: [
      { required: true, message: "Url不能为空", trigger: "blur" }
    ],
    component: [
      { required: true, message: "组件路径不能为空", trigger: "blur" }
    ],
    extend: [
      { required: true, message: "扩展属性", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
    weigh: [
      { required: true, message: "权重(排序)不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态:0=禁用,1=启用不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询会员规则管理列表 */
function getList() {
  loading.value = true;
  listRule(queryParams.value).then(response => {
    ruleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    pid: null,
    type: null,
    title: null,
    name: null,
    path: null,
    icon: null,
    menuType: null,
    url: null,
    component: null,
    extend: null,
    remark: null,
    weigh: null,
    status: null,
    updatetime: null,
    createtime: null
  };
  proxy.resetForm("ruleRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加会员规则管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getRule(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改会员规则管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["ruleRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRule(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRule(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

// 修改状态
function changeSwitchStatus(e,row) {
  updateRule(row).then(response => {
    proxy.$modal.msgSuccess("修改成功");
    // getList();
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除会员规则管理编号为"' + _ids + '"的数据项？').then(function() {
    return delRule(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('userInfo/rule/export', {
    ...queryParams.value
  }, `rule_${new Date().getTime()}.xlsx`)
}

getList();
</script>
